<template>
  <div class="app-container">
    <page-tools search-txt="用户名称" :is-show-add="false" />
    <el-table :data="list" border style="width: 100%">
      <el-table-column
        fixed
        type="index"
        label="序列"
        width="50"
        align="center"
      />
      <el-table-column
        prop="avatar"
        label="用户头像"
        width="150"
        align="center"
      >
        <template v-slot="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.avatar"
            fit="contain"
          />
        </template>
      </el-table-column>
      <el-table-column prop="nickname" label="评论用户" align="center" />
      <el-table-column prop="parent_name" label="回复用户" align="center">
        <template slot-scope="sope">
          {{ sope.row.parent_name ? sope.row.parent_name : "无" }}
        </template>
      </el-table-column>
      <el-table-column prop="title" label="评论文章" align="center" />
      <el-table-column prop="content" label="评论内容" align="center" />
      <el-table-column prop="created_at" label="评论时间" align="center">
        <template slot-scope="sope">
          {{ sope.row.created_at | getTime }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="small"
            @click="onDelComment(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <PagePagination
      :page="pageParams.page"
      :page-size="pageParams.pageSize"
      :total="total"
      @update-page="onUpdatePage"
      @update-size="onUpdateSize"
    />
  </div>
</template>

<script>
import { getCommentListAPI, delCommentByIdAPI } from '@/api/comment'

export default {
  data() {
    return {
      list: [],
      pageParams: {
        page: 1,
        pageSize: 5
      },
      total: 0,
      dialogVisible: false,
      form: {},
      title: ''
    }
  },
  mounted() {
    this.getCommentList()
  },
  methods: {
    async getCommentList() {
      const res = await getCommentListAPI(this.pageParams)
      this.list = res.data.result
      this.total = res.data.total
    },
    onUpdatePage(page) {
      this.pageParams.page = page
      this.getCommentList()
    },
    onUpdateSize(size) {
      this.pageParams.pageSize = size
      this.getCommentList()
    },
    onDelComment(row) {
      console.log(row.id)
      this.$confirm('此操作将永久删除该评论, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await delCommentByIdAPI(row.id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getCommentList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="scss" scoped></style>
